/**
 * 
 */
package com.alstudio.ui.module.user;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;

import com.alstudio.app.ALLocalEnv;
import com.alstudio.ui.base.TitleBarActivity;
import com.alstudio.utils.android.keyboard.ALKeyBoardManager;
import com.alstudio.utils.android.net.http.HttpDataAsynHelper.Callback;
import com.alstudio.utils.android.net.http.HttpResultWrap;
import com.alstudio.utils.formater.ALEmailFormater;
import com.alstudio.view.edittext.ALEditText;
import com.alstudio.view.edittext.ALEditText.OnALEditorActionListener;
import com.alstudio.view.toast.MyToastUtil;
import com.loovee.imaohu.R;

/**
 * 找回密码
 * 
 * @author alonso lee
 * 
 */
public class ForgotPassActivity extends TitleBarActivity implements
		OnALEditorActionListener {

	// 输入框
	private ALEditText inputEditText;

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.alstudio.ui.base.TitleBarActivity#onCreate(android.os.Bundle)
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setupView(this, getClass().getSimpleName());
		initData();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.alstudio.ui.base.BaseActivity#setupView(android.content.Context,
	 * java.lang.String)
	 */
	@Override
	protected void setupView(Context context, String name) {
		// TODO Auto-generated method stub
		super.setupView(context, name);
		setTitleBarContentView(R.layout.forgot_pass_layout);
		setCenterTextViewMessage(R.string.TxtGetPassword);
//		setRightButtonBackground(R.drawable.friend_title_icon_confirm);
		rightUseImageButton(true);
		setRightButtonImageSrc(R.drawable.friend_title_icon_confirm);

		inputEditText = (ALEditText) findViewById(R.id.forgot_pass_mail);
		inputEditText.setALEditorActionListener(this);
		setRightButtonClickListener(this);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.alstudio.ui.base.BaseActivity#initData()
	 */
	@Override
	protected void initData() {
		// TODO Auto-generated method stub
		initUI();
	}

	private void initUI() {
		// 签名只能输入13个字符
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.alstudio.view.edittext.ALEditText.OnALEditorActionListener#onEditorAction
	 * ()
	 */
	@Override
	public void onEditorAction() {
		// TODO Auto-generated method stub

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.alstudio.view.edittext.ALEditText.OnALEditorActionListener#onContentClear
	 * ()
	 */
	@Override
	public void onContentClear() {
		// TODO Auto-generated method stub

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.alstudio.view.edittext.ALEditText.OnALEditorActionListener#
	 * onContentChange(java.lang.String)
	 */
	@Override
	public void onContentChange(String content) {
		// TODO Auto-generated method stub
		boolean isFilled = content.length() != 0;
		getRightButton().setEnabled(isFilled);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.alstudio.ui.base.TitleBarActivity#onClick(android.view.View)
	 */
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		super.onClick(v);
	}
	
	/* (non-Javadoc)
	 * @see com.alstudio.ui.base.TitleBarActivity#invokeRightBtnClick()
	 */
	@Override
	protected void invokeRightBtnClick() {
		// TODO Auto-generated method stub
		super.invokeRightBtnClick();
		invokeRequirePwd();
	}

	private void invokeRequirePwd() {
		// 检测邮箱合法性
		if (!ALEmailFormater.isEmail(inputEditText.getText().toString().trim())) {
			MyToastUtil.getInstance().showToastOnCenter(
					getString(R.string.TxtEmailFormatError));
			inputEditText.requestFocus();
			return;
		}

		if (ALLocalEnv.isNetworkConnected()) {
			// 发送找回密码请求
			ALKeyBoardManager.dismissKeyBoard(this);
			showLoadingProgress();
			com.alstudio.utils.android.net.http.HttpDataAsynHelper
					.forgotPasswordMail(inputEditText.getText().toString()
							.trim(), new Callback() {

						@Override
						public void onSuccess(HttpResultWrap data) {
							// TODO Auto-generated method stub

						}

						@Override
						public void onSuccess(Bundle data) {
							// TODO Auto-generated method stub
							boolean bool = data.getBoolean("isfind");
							Message msg = mHandler.obtainMessage();
							msg.arg1 = 1;
							msg.obj = bool;
							mHandler.sendMessageDelayed(msg, 2000);
						}

						@Override
						public void onError(Exception e) {
							// TODO Auto-generated method stub
							boolean bool = false;
							Message msg = mHandler.obtainMessage();
							msg.arg1 = 1;
							msg.obj = bool;
							mHandler.sendMessageDelayed(msg, 2000);
						}
					});

			// finish();
		}
	}

	private Handler mHandler = new Handler() {

		@Override
		public void handleMessage(android.os.Message msg) {

			if (msg.arg1 == 1) {
				boolean bool = (Boolean) msg.obj;
				dismissLoadingProgress();
				showResetPasswordResult(bool);
				// 发送成功后,返回上一个界面
				if (bool) {
					ForgotPassActivity.this.finish();
				}
			}
		};
	};

	private void showResetPasswordResult(boolean isfind) {

		if (isfind) {
			MyToastUtil.getInstance().showToastOnCenter(
					ForgotPassActivity.this
							.getString(R.string.TxtResetPwdSuccess));
		} else {
			MyToastUtil.getInstance().showToastOnCenter(
					ForgotPassActivity.this
							.getString(R.string.TxtResetPwdFailed));
		}
	}
	
}
